// modules/goodModules/pages/goodDetail/index.js
import {findGood} from '@/api/goods'
import {addOrEdit} from '@/api/cart'
import {checkIsBuy, findPageComment} from '@/api/comment'
import {userBehaviors} from '@/behaviors/userBehaviors'
import {msgTip} from '@/utils/MsgTipApi'
Page({
  behaviors:[userBehaviors],
  /**
   * 页面的初始数据
   */
  data: {
    goodInfo:{},
    goodId:'',
    show:false,
    // 判断是否为购买
    isbuy:null,
    title:'',
    count:1,
    // 评论分页数据
    commentRes:[],
    pageIndex:1,
    pageSize:5,
    totalCount:0
  },
  /**
   * 获取评论的分页数据
   */
  async findPageComment(){
    let params = {pageIndex:this.data.pageIndex,pageSize:this.data.pageSize,goodId:this.data.goodId}
    let res = await findPageComment(params)
    this.setData({
      commentRes:res.records,
      totalCount: res.total
    })
  },
  
  /** 
   * 前往购物车
  */
  goCart(){
    wx.reLaunch({
      url: '/pages/shoppingCart/shoppingCart',
    })
  },
  /**
   * 发布评论
   */
  async pubComment(event){
    if(!this.data.token){
      msgTip({title:'请先登录'})
      return;
    }
    let {id} = event.currentTarget.dataset;
    let res = await checkIsBuy(id)
    if(res == true){
      wx.navigateTo({
        url: `/modules/commentModules/pages/commentPub/index?goodId=${id}`,
      })
    }else{
      msgTip({title:'未购买此商品，无法评论'})
    }
  },
  addOrdereCar(){
    if(!this.data.token) {
      msgTip({title:'尚未登录，请先登录'})
      return
    }
    this.setData({
      show:true,
      isbuy:false,
      title:'加入购物车'
    })
  },
  async sureAddOrderCar(){
    let reqData  ={id:null,goodId:this.data.goodInfo.id, count:this.data.count}
    let data = await addOrEdit(reqData);
    if(data){
      msgTip({title:'加入成功'})
    }else{
      msgTip({title:'未知错误，请联系管理员'})
    }
  },
  async buyGood(){
    if(!this.data.token) {
      msgTip({title:'尚未登录，请先登录'})
      return
    }
    this.setData({show:true,isbuy:true,title:'购买商品'})
  },
  handlerSubmit(){
    if(this.data.isbuy){
      wx.navigateTo({
        url: `/modules/orderModules/pages/orderSubmit/index?goodId=${this.data.goodInfo.id}&count=${this.data.count}&goodName=${this.data.goodInfo.goodName}&goodPrice=${this.data.goodInfo.goodPrice}&goodImgUrl=${this.data.goodInfo.goodImgUrl}&stockNum=${this.data.goodInfo.totalNum} `,
      })
    }else{
      this.sureAddOrderCar()
      this.onClose()
    }
  },
  onClose(){
    this.setData({isbuy:null,show:false,title:'',count:1})
  },
  onCountChange(event){
    this.setData({count:event.detail})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let id = options.goodId
    this.data.goodId = id;
    if(id){
      let data = await findGood(options.goodId)
      this.setData({
        goodInfo: data
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.findPageComment()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})